<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Api_Model_CommentMapper {
	protected $_db_table;

	public function __construct() {
		$this->_db_table = new Api_Model_DbTable_Comment();
	}

	public function getAllComment($photo_id, $comment_limit) {
		$select = $this->_db_table->select()->from('comments', array('*'))->where('photo_id = ?', $photo_id)
		->order('created_time')->limit($comment_limit+30, $comment_limit);

		$result = $this->_db_table->fetchAll($select);

		$db_user = new Api_Model_DbTable_User();

		if(count($result) == 0) {
			throw new Exception(json_encode(array('meta' => 'Not found')),404);
		} else {
			foreach ($result as $row) {
				// $row should be a Zend_Db_Table_Row object
				// cast to array
				// $rowArray = $row->toArray();

				$select_user = $db_user->select()->from('users', array('*'))->where('user_id = ?', $row->user_id);
				$result_user = $this->_db_table->fetchAll($select_user);
				$row_user = $result_user->current();
				//get user info
				$user = array('user_id' => $row_user->user_id,
                              'username' => $row_user->username,
                              'fullname' => $row_user->fullname,
                              'profile_picture' => $row_user->profile_picture
				);

				$comments[] = array('comment' => array('comment_id' => $row->comment_id,
                                                    'content' => $row->content,
                                                    'created_time' => $row->created_time,
                                                    'user' => $user));

			}
			return $comments;
		}
	}

	public function sendComment($data){

		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$currentDateTime = date('Y-m-d H-i-s');
		$comment = array('photo_id' => $data['photo_id'],
                        'user_id' => $data['user_id'],
                        'content' => $data['content'],
                        'location_id' => $data['location_id']);

		$affectedRow = $this->_db_table->insert($comment);
		$db_user = new Api_Model_DbTable_User();

		if ($affectedRow){
			$comment_id = $this->_db_table->getAdapter()->lastInsertId();
			$this->_db_table->update(array('created_time' => $currentDateTime),
			array('comment_id = ?' => $comment_id));

			$select = $this->_db_table->select()->from('comments', array('*'))->where('comment_id = ?', $comment_id);
			$result = $this->_db_table->fetchAll($select);
			$row_cm = $result->current();

			$select_user = $db_user->select()->from('users', array('*'))->where('user_id = ?', $data['user_id']);
			$result_user = $this->_db_table->fetchAll($select_user);
			$row_user = $result_user->current();
			//get user info
			$user = array('user_id' => $row_user->user_id,
                          'username' => $row_user->username,
                          'fullname' => $row_user->fullname,
                          'profile_picture' => $row_user->profile_picture
			);

			$json = array('comment_id' => $row_cm->comment_id,
                                    'content' => $row_cm->content,
                                    'created_time' => $row_cm->created_time,
                                    'user' => $user);

			return $json;


		} else {
			throw new Exception('Internal Server Error' ,500);
		}
	}

}